$(function () {
    $("#jqGrid").jqGrid({
        url: '../companyinfo/list',     // 请求后台json数据的url
        datatype: "json",                // 后台返回的数据格式
        // 列表标题及列表模型
        colModel: [
            {label: '公司ID', name: 'companyid', index: 'COMPANYID', width: 50, key: true},
            {label: '公司名称', name: 'companyname', index: 'COMPANYNAME', width: 80},
            {label: '公司联系人', name: 'companyaccount', index: 'COMPANYACCOUNT', width: 80},
            {label: '联系人电话', name: 'phoneno', index: 'PHONENO', width: 80},
            {label: '企业邮箱', name: 'email', index: 'EMAIL', width: 80},
            {label: '状态', name: 'status', index: 'STATUS', width: 80, formatter: vm.customFmatter},
            {label: '加入时间', name: 'createtime', index: 'CREATETIME', width: 80}

        ],
        viewrecords: true,     // 是否显示行号，默认值是false，不显示
        height: 385,            // 表格高度
        rowNum: 50,             // 一页显示的行记录数
        rowList: [50, 100],     // 翻页控制条中 每页显示记录数可选集合
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",          // 翻页DOM
        jsonReader: {                   // 重写后台返回数据的属性
            root: "page.list",          // 将rows修改为page.list
            page: "page.currPage",      // 将page修改为page.currPage
            total: "page.totalPage",    // 将total修改为page.totalPage
            records: "page.totalCount"  // 将records修改为page.totalCount
        },
        prmNames: {              // 改写请求参数属性
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
            //设置高度
            $("#jqGrid").jqGrid('setGridHeight', getWinH());
        }
    });


});

var vm = new Vue({
        el: '#rrapp',
        data: {
            showList: true,
            title: null,
            companyinfo: {},
            bindmodal: false
        },
        methods: {
            query: function () {
                $("#jqGrid").jqGrid('setGridParam', {
                    postData: {
                        keyword: $("#keyword").val()
                    },
                    page: 1
                }).trigger("reloadGrid");

            },
            add: function () {
                vm.showList = false;
                vm.title = "新增1";
                vm.companyinfo = {};
            },
            update: function (event) {
                var companyid = getSelectedRow();
                if (companyid == null) {
                    return;
                }
                vm.showList = false;
                vm.title = "修改";

                vm.getInfo(companyid)
            },
            saveOrUpdate: function (event) {
                var url = vm.companyinfo.companyid == null ? "../companyinfo/save" : "../companyinfo/update";
                $.ajax({
                    type: "POST",
                    url: url,
                    data: JSON.stringify(vm.companyinfo),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            },
            del: function (event) {
                var companyids = getSelectedRows();
                if (companyids == null) {
                    return;
                }

                confirm('确定要删除选中的记录？', function () {
                    $.ajax({
                        type: "POST",
                        url: "../companyinfo/delete",
                        data: JSON.stringify(companyids),
                        success: function (r) {
                            if (r.code == 0) {
                                alert('操作成功', function (index) {
                                    $("#jqGrid").trigger("reloadGrid");
                                });
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                });
            },
            getInfo: function (companyid) {
                $.get("../companyinfo/info/" + companyid, function (r) {
                    vm.companyinfo = r.companyinfo;
                    //更新输入框高度
                    autosize.update($('textarea'));
                });
            },
            reload: function (event) {
                vm.showList = true;
                var page = $("#jqGrid").jqGrid('getGridParam', 'page');
                $("#jqGrid").jqGrid('setGridParam', {
                    page: page
                }).trigger("reloadGrid");
            },
            bind: function () {
                var companyid = getSelectedRow();
                if(companyid != null && companyid != ''){
                    vm.querybind(companyid);
                    vm.otherBind(companyid);
                    $('#myModal').modal({
                        keyboard: true
                    });
                }
                vm.title = "绑定设备";
                vm.companyinfo = {};
                vm.bindmodal = true;

            },

            querybind: function (companyid) {
                console.log(companyid);
                var url1 = '../companyinfo/bindDeviceList/' + companyid;
                console.log(url1);

                $("#jqGridBind").jqGrid({
                    url: url1,     // 请求后台json数据的url
                    datatype: "json",                // 后台返回的数据格式
                    // 列表标题及列表模型
                    colModel: [
                        {label: '设备编号', name: 'DEVICEID', index: 'DEVICEID', width: 200, key: true},
                        {label: '设备厂商', name: 'DEVICEDESC', index: 'DEVICEDESC', width: 200},
                        {label: '设备型号', name: 'DEVICETYPE', index: 'DEVICETYPE', width: 100},
                        {label: '内线号码', name: 'ZWANNUMBER', index: 'ZWANNUMBER', width: 100},
                        {label: '登录密码', name: 'PASSWORD', index: 'PASSWORD', width: 100}

                    ],
                    viewrecords: true,     // 是否显示行号，默认值是false，不显示
                    height: 385,
                    width: 800,
                    shrinkToFit: true, // 表格高度
                    rowNum: 50,             // 一页显示的行记录数
                    rowList: [50, 100],     // 翻页控制条中 每页显示记录数可选集合
                    rownumbers: true,
                    rownumWidth: 25,
                    autowidth: true,
                    multiselect: true,
                    pager: "#jqGridPagerBind",          // 翻页DOM
                    jsonReader: {                   // 重写后台返回数据的属性
                        root: "page.list",          // 将rows修改为page.list
                        page: "page.currPage",      // 将page修改为page.currPage
                        total: "page.totalPage",    // 将total修改为page.totalPage
                        records: "page.totalCount"  // 将records修改为page.totalCount
                    },
                    prmNames: {              // 改写请求参数属性
                        page: "page",
                        rows: "limit",
                        order: "order"
                    },
                    gridComplete: function () {
                        //隐藏grid底部滚动条
                        $("#jqGridBind").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
                        //设置高度
                        $("#jqGridBind").jqGrid('setGridHeight', getWinH());
                        $("#jqGridBind").jqGrid('setGridWidth', 800);
                    }
                });
            },
            querybinddevice: function () {
                $("#jqGridBind").jqGrid('setGridParam', {
                    postData: {
                        keyword: $("#keywordbind").val()
                    },
                    page: 1
                }).trigger("reloadGrid");

            },
            otherBind: function (companyid) {
                $("#jqGridBind").jqGrid('setGridParam', {
                  url : '../companyinfo/bindDeviceList/' + companyid
                }).trigger("reloadGrid");

            },

            addbind: function () {
                var companyid = getSelectedRow();
                $('#addBindmodal').modal({
                    keyboard: true
                });
                vm.querynotbind(companyid);
            },
            querynotbind: function (companyid) {
                console.log(companyid);
                var url1 = '../companyinfo/notBindList';
                console.log(url1);

                $("#jqGridAddBind").jqGrid({
                    url: url1,     // 请求后台json数据的url
                    datatype: "json",                // 后台返回的数据格式
                    // 列表标题及列表模型
                    colModel: [
                        {label: '设备序号', name: 'deviceseqno', index: 'DEVICESEQNO', width: 50, key: true},
                        {label: '批次', name: 'batchid', index: 'BATCHID', width: 80},
                        {label: '厂商', name: 'devicevendorid', index: 'DEVICEVENDORID', width: 80},
                        {label: '类型', name: 'devicetype', index: 'DEVICETYPE', width: 80},
                        {label: '设备编号', name: 'deviceid', index: 'DEVICEID', width: 80},
                        {label: '描述', name: 'devicedesc', index: 'DEVICEDESC', width: 80}

                    ],
                    viewrecords: true,     // 是否显示行号，默认值是false，不显示
                    height: 385,
                    width: 800,
                    shrinkToFit: true, // 表格高度
                    rowNum: 50,             // 一页显示的行记录数
                    rowList: [50, 100],     // 翻页控制条中 每页显示记录数可选集合
                    rownumbers: true,
                    rownumWidth: 25,
                    autowidth: true,
                    multiselect: true,
                    //  pager: "#jqGridPagerBind",          // 翻页DOM
                    jsonReader: {                   // 重写后台返回数据的属性
                        root: "sysorderdeviceinfoList",          // 将rows修改为page.list
                        page: "page.currPage",      // 将page修改为page.currPage
                        total: "page.totalPage",    // 将total修改为page.totalPage
                        records: "page.totalCount"  // 将records修改为page.totalCount
                    },
                    prmNames: {              // 改写请求参数属性
                        page: "page",
                        rows: "limit",
                        order: "order"
                    },
                    gridComplete: function () {
                        //隐藏grid底部滚动条
                        $("#jqGridAddBind").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
                        //设置高度
                        $("#jqGridAddBind").jqGrid('setGridHeight', getWinH());
                        $("#jqGridAddBind").jqGrid('setGridWidth', 800);
                    }
                });
            },
            activeDevice: function () {
                var params = {
                    "companyid": "",
                    "devicevendorid": "",
                    "devicetype": "",
                    "deviceid": ""
                };
                params.companyid = getSelectedRow();
                var rowData = $("#jqGridAddBind").getRowData(vm.getGridSelectedRow("jqGridAddBind"));
                params.devicevendorid = rowData.devicevendorid;
                params.devicetype = rowData.devicetype;
                params.deviceid = rowData.deviceid;


                $.ajax({
                    url: "../companyinfo/activeDevice",
                    data: {
                        companyid: params.companyid,
                        devicevendorid: params.devicevendorid,
                        devicetype: params.devicetype,
                        deviceid: params.deviceid
                    },
                    success: function (r) {
                        if (r.code == 0) {
                            $('#addBindmodal').modal('hide');
                        } else {
                            alert(r.msg);
                        }
                    }
                });

            },

            delbind: function () {
                var params = {
                    "companyid": "",
                    "devicevendorid": "",
                    "devicetype": "",
                    "deviceid": ""
                };
                params.companyid = getSelectedRow();
                var rowData = $("#jqGridBind").getRowData(vm.getGridSelectedRow("jqGridBind"));
                params.deviceid = rowData.DEVICEID;


                $.ajax({
                    url: "../companyinfo/delDevice",
                    data: {
                        companyid: params.companyid,
                        deviceid: params.deviceid
                    },
                    success: function (r) {
                        if (r.code == 0) {
                            alert(r.msg);
                            $('#myModal').modal('hide');
                        } else {
                            alert(r.msg);
                        }
                    }
                });

            },

            customFmatter: function (cellvalue, options, rowObject) {
                switch (cellvalue) {

                    case 1:

                        return "正常";

                        break;

                    case 2:

                        return "暂停";

                        break;


                    default:

                        return "销户";

                }
            },
//选择一条记录
            getGridSelectedRow: function (grid) {
                var grid = $("#" + grid);
                var rowKey = grid.getGridParam("selrow");
                if (!rowKey) {
                    alert("请选择一条记录");
                    return;
                }

                var selectedIDs = grid.getGridParam("selarrrow");
                if (selectedIDs.length > 1) {
                    alert("只能选择一条记录");
                    return;
                }

                return selectedIDs[0];
            }

        }
    })
;

// 图片自适应


// textarea 高度自动扩展
autosize($('textarea'));